package Bi_Tree.level_traversal;

import Bi_Tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class lc102_base {
    public static void main(String[] args) {

    }
    public List<List<Integer>> levelOrder(TreeNode root) { //leetcode版
        List<List<Integer>> result=new ArrayList<>();
        if (root==null) {
            return result;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        TreeNode node=root;
        queue.offer(node);//入队
        while(!queue.isEmpty()){
            List<Integer> samlllist=new ArrayList<>();
            int len= queue.size();
            while(len!=0){
                node= queue.poll();//出队
                samlllist.add(node.val);
                if(node.left!=null) queue.offer(node.left);
                if(node.right!=null) queue.offer(node.right);
                len--;
            }
            result.add(samlllist);
        }

        return result;

    }

    public List<Integer> levelOrder_norm(TreeNode root){ //普通版
        List<Integer> result=new ArrayList<>();
        if (root==null) {
            return result;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        TreeNode node=root;
        queue.offer(node);//入队
        while(!queue.isEmpty()){
            node= queue.poll();
            result.add(node.val);
            if(node.left!=null) queue.offer(node.left);
            if(node.right!=null) queue.offer(node.right);
        }
        return result;

    }
}
